/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author computer
 */
public class delAcct extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    int delID, isAdmin = 0, UID = 0;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException, Exception {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {       
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Threadr || Deleting account...</title>");
            out.println("</head>");
            out.println("<body>");
            
            Connect.init();
            Connection conn = Connect.getConnection();
            HttpSession session = request.getSession(true);
            Statement stat = conn.createStatement();
            Statement stat2 = conn.createStatement();

            try
            {
               if (request.getParameter("id")!= null) {
                   UID = (Integer)session.getAttribute("uID");
                   delID = Integer.parseInt(request.getParameter("id"));
                   ResultSet user = stat.executeQuery("SELECT * FROM t_Users WHERE UID = "+UID);
                   while(user.next())
                       isAdmin = user.getInt("isAdmin");
                   if (delID == UID || isAdmin == 1) {
                       stat2.executeUpdate("UPDATE t_Users SET deleted = 1 WHERE UID = " + delID);
                       out.println("ACCOUNT DELETED");
                   }
                   else
                       out.println("INVALID CREDENTIALS");
               } else if (session.getAttribute("uID")!=null) {
                   stat2.executeUpdate("UPDATE t_Users SET deleted = 1 WHERE UID = " + (Integer)session.getAttribute("uID"));
                   session.invalidate();
                   out.println("ACCOUNT DELETED");
               } else
                out.println("NOT LOGGED IN");
            }
            finally
            {
                conn.close();
            }           
        } finally {
            out.println("<br/><a href=\"index\">Back to homepage</a>");
            out.println("</body>");
            out.println("</html>");
            out.close();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    //@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    //@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Login Servlet";
    }// </editor-fold>
}